/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","oms_config/root",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",],function($,$bb,$root,$datatableServers,$datatableUi){
    var servers = {};
    var URL = $bb.baseUrl("followup");
    /**
     * 入参
     */
    var inParams = function(args){
        var params = {
            "args": JSON.stringify(args),
            "flag": 3,
            "patientName": $.trim($('#searchByName').val()),
            "hisNo": $.trim($('#searchByHisNo').val()),
            "phoneNum": $.trim($('#searchByPhone').val()),
        }
        var config = $root.init();
        if( config.dom.outpatientRecordChkAge ){
            var chkAgeFrom, chkAgeTo;
            if($("#ageFrom-year").val() !="" || $("#ageFrom-month").val() !=""){
                chkAgeFrom = parseInt($("#ageFrom-year").val()*365) + parseInt($("#ageFrom-month").val()*30);
            }else{
                chkAgeFrom = "";
            }
            if($("#ageTo-year").val() !="" || $("#ageTo-month").val() !=""){
                chkAgeTo = parseInt($("#ageTo-year").val()*365) + parseInt($("#ageTo-month").val()*30);
            }else{
                chkAgeTo = "";
            }
            $.extend(params,{
                "chkAgeFrom": chkAgeFrom,
                "chkAgeTo": chkAgeTo,
                "taskAgeFlag": "Y"
            });
        }
        var data = JSON.stringify({ data: params });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : e.hisNo3 ? e.hisNo3 : "-";
    }
    var columnsPatientName = function(e){
        var imgSrc = 'page/followup/imgs/comm/';
        imgSrc += e.gender == 'F' ? 'girl.png' : 'boy.png';
        var h = '<img class="pic-sex" src='+imgSrc+'>';
            h += '<span class="patient-name">' + e.patientName + '</span>'; 
        return h;
    }
    var columnsFollowQueueName = function(e){
        return '<span class="followQueue-name">'+e.followQueueName+'</span>';
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":columnsHisNo},
            {"data":columnsPatientName},
            {"data":'phoneNum'},
            {"data":'birthday'},
            {"data":'taskName'},
            {"data":columnsFollowQueueName},
            {
                "data":"chkAge",
                "className":"chk-age"
            },
            {"data":'updateTime'},
            {"data":'updateAuname'},
            {
                "data": null,
                "className":"operate"
            },
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = '<div class="btn-look">查看</div>';
        $('td.operate', row).html(h);
        $("td.operate .btn-look",row).unbind("click");
        $("td.operate .btn-look",row).click(function(){
            var imgSrc = 'page/followup/imgs/comm/' + (data.gender == 'F'? 'girl.png':'boy.png');
            var params = {
                patientSchemeId: data.patientSchemeId,
                followQueueId: data.followQueueId,
                patientName: data.patientName,
                imgSrc: imgSrc,
                queuename: data.followQueueName,
                patientId: data.patientId,
                taskId: data.taskId,
            };
            $bb.openPage("outPatient",params);
        });          
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#outpatientRecord").prev("div.row").remove();
        $("#searchByName, #searchByHisNo, #searchByPhone, #ageFrom-year, #ageFrom-month, #ageTo-year, #ageTo-month").bind("input propertychange",function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/patientFollowDealRecordList?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

    return servers;
});